<?php

namespace app\admin\controller\order;

use app\common\controller\Backend;

class Refund extends Backend{
    public function __construct()
    {
        parent::__construct();

        $this->ApplyModel=model('Order.Apply');
        $this->OrderModel=model('Order.Order');
    }

    public function index(){
        $applylist=$this->ApplyModel->with(['users'])->paginate(10);

        $count=$this->ApplyModel->count();

        $pagestr=$applylist->render();

        $this->assign([
            'applylist'=>$applylist,
            'count'=>$count,
            'pagestr'=>$pagestr,
        ]);

        return $this->fetch();
    }

    public function pass()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isAjax())
        {
            
            $ids = $this->request->param('ids',0);
            
            $apply = $this->ApplyModel->find($ids);

            $this->ApplyModel->startTrans();
            $this->OrderModel->startTrans();

            $ApplyData=[
                'id'=>$ids,
                'status'=>'1'
            ];

            $ApplyStatus=$this->ApplyModel->isUpdate(true)->save($ApplyData);
            // var_dump($ApplyStatus);exit;

            if($ApplyStatus === FALSE){
                $success['result'] = false;
                $success['msg'] = $this->ApplyModel->getError();
                return json($success);
            }

            $OrderData=[
                'id'=>$apply['orderid'],
                'status'=>'-1'
            ];

            $OrderStatus=$this->OrderModel->isUpdate(true)->save($OrderData);

            // echo $this->OrderModel->getLastSql();
            // exit;
            // var_dump($OrderStatus, $OrderData);
            // exit;

            if($OrderStatus === FALSE){
                $this->ApplyModel->rollback();
                $success['result'] = false;
                $success['msg'] = $this->OrderModel->getError();
                return json($success);
            }

            if($ApplyStatus===FALSE || $OrderStatus===FALSE){
                $this->OrderModel->rollback();
                $this->ApplyModel->rollback();
                $success['result'] = false;
                $success['msg'] = '审核通过失败';
                return json($success);
            }else{
                $this->OrderModel->commit();
                $this->ApplyModel->commit();
                $success['result'] = true;
                $success['msg'] = '审核通过成功';
                return json($success);
            }
        }
    }

    public function onpass()
    {
        $success = [
            'result' => false,
            'msg' => ''
        ];

        if($this->request->isAjax())
        {
            $ids = $this->request->param('ids',0);

            $apply = $this->ApplyModel->find($ids);

            $this->ApplyModel->startTrans();
            $this->OrderModel->startTrans();

            $ApplyData=[
                'id'=>$ids,
                'status'=>-1
            ];

            $ApplyStatus=$this->ApplyModel->isUpdate(true)->save($ApplyData);

            if($ApplyStatus === FALSE){
                $success['result'] = false;
                $success['msg'] = $this->ApplyModel->getError();
            }

            $OrderData=[
                'id'=>$apply['orderid'],
                'status'=>2
            ];

            $OrderStatus=$this->OrderModel->isUpdate(true)->save($OrderData);

            if($OrderStatus === FALSE){
                $this->ApplyModel->rollback();
                $success['result'] = false;
                $success['msg'] = $this->OrderModel->getError();
            }

            if($ApplyStatus===FALSE || $OrderStatus===FALSE){
                $this->OrderModel->rollback();
                $this->ApplyModel->rollback();
                $success['result'] = false;
                $success['msg'] = '审核不通过失败';
            }else{
                $this->OrderModel->commit();
                $this->ApplyModel->commit();
                $success['result'] = true;
                $success['msg'] = '审核通过成功';
            }
            return json($success);
        }
    }
}